package com.jiazhong.example.project.shoppingcart.filter;

import com.jiazhong.example.project.shoppingcart.util.DateUtil;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

//@WebFilter("/*")
public class LogFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        System.out.println("开始写日志");
        String uri = req.getRequestURI();
        // message.log 是在classes里边
        File file = new File("message.log");
        System.out.println("写入到：" + file.getAbsolutePath());
        Writer writer = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(writer);
        String message = DateUtil.nowDate() + "：" + uri + "\n";
        bw.write(message);
        bw.flush();
        bw.close();
        chain.doFilter(req, res);
    }
}
